import android
from time import sleep
from math import degrees
def compass():
    droid = android.Android()

    while(1):
      droid.startSensing()
      sleep(2)
      sense = droid.readSensors()
#print sense[1]
      print sense[1]['azimuth']
      azimuth = sense[1]['azimuth']
      droid.stopSensing()
      degree = round(degrees(azimuth))
      if (degree in range(-22.5,22.5)):
        print "north"
      elif(degree in range(22.5,67.5)):
        print "north east"
      elif(degree in range(67.5,112.5)):
        print "east"
      elif(degree in range(112.5,157.5)):
        print "south east"
      elif(degree in range(157.5,180) or degree in range(-180,-157.5)):
        print "south"
      elif(degree in range(-157.5,-112.5)):
        print "south west"
      elif(degree in range(-112.5,-67.5)):
        print "west"
      elif(degree in range(-67.5,-22.5)):
        print "north west"

def GetAzimuth():
    droid = android.Android()
    droid.startSensing()
    sleep(2)
    sense = droid.readSensors()
    azimuth = sense[1]['azimuth']
    droid.stopSensing()
    degree = round(degrees(azimuth))
    return azimuth
